//
//  RectangleRender.m
//  MyOpenGLESLearn_iOS
//
//  Created by Gaomingyang on 2021/4/13.
//

#import "RectangleRender.h"
#import "ShaderProgram.h"


#if TARGET_IPHONE_SIMULATOR || TARGET_OS_IPHONE
NSString *const kNormalReactVertexShaderString = SHADER_STRING
(
 attribute vec3 vPosition;//顶点位置
 void main(){
        gl_Position = vec4(vPosition,1);
    }
 );
#else

#endif

#if TARGET_IPHONE_SIMULATOR || TARGET_OS_IPHONE
NSString *const kNormalReactFragmentShaderString = SHADER_STRING
(
 precision mediump float;
 varying vec4 vDiffuse;//接受从顶点着色器过来的散射光分量

 void main(){
     vec4 finalColor = vec4(1.0);
//     //给此片元着色值
    gl_FragColor = finalColor;
 }
 );
#else

#endif

@interface RectangleRender()<GLKViewDelegate>
{
    // Render
    GLuint  _program;
}
@property (strong, nonatomic) ShaderProgram* shaderProcessor;
@property (strong, nonatomic) EAGLContext* context;
@property (strong, nonatomic) GLKView* glkView;
@end
struct AttributeHandles
{
    GLint   vPosition;
};
@implementation RectangleRender
{
    struct AttributeHandles _attributes;
}
- (instancetype)initWithGLKView:(GLKView *)glkView{
    self = [super init];
    if (self) {
        [self commonInitWithGLKView:glkView];
    }
    return self;
}
-(void)commonInitWithGLKView:(GLKView *)glkView{
    [self initOpenglESWithGLKView:glkView];
}
-(void)initOpenglESWithGLKView:(GLKView *)glkView{
    self.context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
    
    // Setup View
    self.glkView = glkView;
    self.glkView.delegate = self;
    self.glkView.context = self.context;
    self.glkView.opaque = YES;
    self.glkView.drawableColorFormat = GLKViewDrawableColorFormatRGBA8888;
    self.glkView.drawableDepthFormat = GLKViewDrawableDepthFormat16;
    
    // Initialize Class Objects
    self.shaderProcessor = [[ShaderProgram alloc] init];
    [self resetupOpenGLES];
}
- (void)resetupOpenGLES
{
    [EAGLContext setCurrentContext:self.context];
    // Enable depth test
    glEnable(GL_DEPTH_TEST);
    // Create the GLSL program
    _program = [self.shaderProcessor BuildProgram:[kNormalReactVertexShaderString cStringUsingEncoding:NSUTF8StringEncoding] with:[kNormalReactFragmentShaderString cStringUsingEncoding:NSUTF8StringEncoding]];

    // end create program
    glUseProgram(_program);
    
    // Extract the attribute handles
    _attributes.vPosition = glGetAttribLocation(_program, "vPosition");

}
- (void)setDisplay{
    [self.glkView display];
}
# pragma mark - GLKView Delegate
- (void)update:(id)sender {
    [self.glkView display];
}

- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect
{
    // Clear Buffers
    glClearColor(0.05, 0.05, 0.05, 1.0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    
    // Enable Attributes
    glEnableVertexAttribArray(_attributes.vPosition);
    
    float rectangleCoords[12] = {
               -0.5f, 0.5f, 0.0f,//top left
               -0.5f, -0.5f, 0.0f, // bottom left
               0.5f, -0.5f, 0.0f, // bottom right
               0.5f, 0.5f, 0.0f // top right
       };
    short indices[6] = {
                0, 1, 2, 0, 2, 3
        };
    glVertexAttribPointer(_attributes.vPosition, 3, GL_FLOAT, GL_FALSE, 0,rectangleCoords);

//    glDrawArrays(GL_TRIANGLES, 0, 4);
    glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices);
    
    // Disable Attributes
    glDisableVertexAttribArray(_attributes.vPosition);
    
}

# pragma mark - GLKViewController Delegate

- (void)glkViewControllerUpdate:(GLKViewController *)controller
{
}
@end
